package com.ruoyi.web.controller.prorepairengineers;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ProRepairMoneys;
import com.ruoyi.service.IProRepairMoneysService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 维修器件金额Controller
 * 
 * @author ruoyi
 * @date 2020-07-09
 */
@Controller
@RequestMapping("/ruoyi/formoneys")
public class ProRepairForMoneysController extends BaseController
{
    private String prefix = "ruoyi/moneys";

    @Autowired
    private IProRepairMoneysService proRepairMoneysService;

    @RequiresPermissions("ruoyi:moneys:view")
    @GetMapping("/moneys/{mPid}")
    public String moneys(@PathVariable("mPid") Long mPid,ModelMap mmap)
    {
        mmap.put("mPid",mPid);
        return prefix + "/formoney";
    }


    /**
     * 查询维修器件金额列表
     */
    @RequiresPermissions("ruoyi:moneys:list")
    @PostMapping("/lists/{mPid}")
    @ResponseBody
    public TableDataInfo lists(@PathVariable("mPid") Long mPid)
    {
        startPage();
        List<ProRepairMoneys> list = proRepairMoneysService.selectMoneysIdList(mPid);
        return getDataTable(list);
    }

    /**
     * 查询维修器件金额列表
     */
    @RequiresPermissions("ruoyi:moneys:list")
    @PostMapping("/list/{mPid}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("mPid") Long mPid,ProRepairMoneys proRepairMoneys)
    {
        proRepairMoneys.setmPid(mPid);
        startPage();
        List<ProRepairMoneys> list = proRepairMoneysService.selectProRepairMoneysList(proRepairMoneys);
        return getDataTable(list);
    }

    /**
     * 导出维修器件金额列表
     */
    @RequiresPermissions("ruoyi:moneys:export")
    @Log(title = "维修器件金额", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{mPid}")
    @ResponseBody
    public AjaxResult export(@PathVariable("mPid")Long mPid,ProRepairMoneys proRepairMoneys)
    {
        proRepairMoneys.setmPid(mPid);
        List<ProRepairMoneys> list = proRepairMoneysService.selectProRepairMoneysList(proRepairMoneys);
        ExcelUtil<ProRepairMoneys> util = new ExcelUtil<ProRepairMoneys>(ProRepairMoneys.class);
        return util.exportExcel(list, "维修器件金额");
    }

    /**
     * 新增维修器件金额
     */
    @GetMapping("/add/{mPid}")
    public String add(@PathVariable("mPid") Long mPid, ModelMap mmap)
    {
        mmap.put("mPid",mPid);
        return prefix + "/add";
    }

    /**
     * 新增保存维修器件金额
     */
    @RequiresPermissions("ruoyi:moneys:add")
    @Log(title = "维修器件金额", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProRepairMoneys proRepairMoneys)
    {

        if (proRepairMoneys.getpName()!=null&&proRepairMoneys.getpPrice()!=null&&proRepairMoneys.getpNumber()!=null&&proRepairMoneys.getpNdate()!=null){
//            Double prices=proRepairMoneys.getpPrice();
            Long number=proRepairMoneys.getpNumber();
            BigDecimal num=BigDecimal.valueOf(number);
//            Double c=prices*number;
            proRepairMoneys.setpSumprice(proRepairMoneys.getpPrice().multiply(num));
            return toAjax(proRepairMoneysService.insertProRepairMoneys(proRepairMoneys));
        }else{
            return toAjax(0);
        }
    }

    /**
     * 修改维修器件金额
     */
    @GetMapping("/edit/{pId}")
    public String edit(@PathVariable("pId") Long pId, ModelMap mmap)
    {
        ProRepairMoneys proRepairMoneys = proRepairMoneysService.selectProRepairMoneysById(pId);
        mmap.put("proRepairMoneys", proRepairMoneys);
        return prefix + "/edit";
    }

    /**
     * 修改保存维修器件金额
     */
    @RequiresPermissions("ruoyi:moneys:edit")
    @Log(title = "维修器件金额", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProRepairMoneys proRepairMoneys)
    {
//        Double prices=proRepairMoneys.getpPrice();
        Long number=proRepairMoneys.getpNumber();
        BigDecimal num=BigDecimal.valueOf(number);
//        ;
//        Double c=prices*number;
        proRepairMoneys.setpSumprice(proRepairMoneys.getpPrice().multiply(num));
        return toAjax(proRepairMoneysService.updateProRepairMoneys(proRepairMoneys));
    }

    /**
     * 删除维修器件金额
     */
    @RequiresPermissions("ruoyi:moneys:remove")
    @Log(title = "维修器件金额", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(proRepairMoneysService.deleteProRepairMoneysByIds(ids));
    }
}
